home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
lxml
/
_elementpath.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
5KB
|
226 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
import re
xpath_tokenizer = re.compile('(\'[^\']*\'|"[^"]*"|::|//?|\\.\\.|\\(\\)|[/.*:\\[\\]\\(\\)@=])|((?:\\{[^}]+\\})?[^/:\\[\\]\\(\\)@=\\s]+)|\\s+').findall
def prepare_tag(next, token):
tag = token[1]
def select(result):
for elem in result:
for e in elem.iterchildren(tag = tag):
yield e
return select
def prepare_star(next, token):
def select(result):
for elem in result:
for e in elem:
yield e
return select
def prepare_dot(next, token):
def select(result):
return result
return select
def prepare_iter(next, token):
token = next()
if token[0] == '*':
tag = '*'
elif not token[0]:
tag = token[1]
else:
raise SyntaxError
def select(result):
for elem in result:
for e in elem.iterdescendants(tag = tag):
yield e
return select
def prepare_dot_dot(next, token):
def select(result):
for elem in result:
parent = elem.getparent()
if parent is not None:
yield parent
continue
return select
def prepare_predicate(next, token):
token = next()
if token[0] == '@':
token = next()
if token[0]:
raise SyntaxError('invalid attribute predicate')
key = token[1]
token = next()
if token[0] == ']':
def select(result):
for elem in result:
if elem.get(key) is not None:
yield elem
continue
elif token[0] == '=':
value = next()[0]
if value[:1] == "'" or value[:1] == '"':
value = value[1:-1]
else:
raise SyntaxError('invalid comparison target')
token = next()
def select(result):
for elem in result:
if elem.get(key) == value:
yield elem
continue
if token[0] != ']':
raise SyntaxError('invalid attribute predicate')
elif not token[0]:
tag = token[1]
token = next()
if token[0] != ']':
raise SyntaxError('invalid node predicate')
def select(result):
for elem in result:
if find(elem, tag) is not None:
yield elem
continue
else:
raise SyntaxError('invalid predicate')
return select
ops = {
'': prepare_tag,
'*': prepare_star,
'.': prepare_dot,
'..': prepare_dot_dot,
'//': prepare_iter,
'[': prepare_predicate }
_cache = { }
def _build_path_iterator(path):
try:
return _cache[path]
except KeyError:
pass
if len(_cache) > 100:
_cache.clear()
if path[:1] == '/':
raise SyntaxError('cannot use absolute path on element')
stream = iter(xpath_tokenizer(path))
try:
_next = stream.next
except AttributeError:
def _next():
return next(stream)
except:
(None,)
token = _next()
selector = []
while None:
try:
selector.append(ops[token[0]](_next, token))
except StopIteration:
(None,)
(None,)
raise SyntaxError('invalid path')
except:
(None,)
try:
token = _next()
if token[0] == '/':
token = _next()
continue
except StopIteration:
(None,)
(None,)
break
continue
return selector
def iterfind(elem, path):
selector = _build_path_iterator(path)
result = iter((elem,))
for select in selector:
result = select(result)
return result
def find(elem, path):
it = iterfind(elem, path)
try:
try:
_next = it.next
except AttributeError:
return next(it)
return _next()
except StopIteration:
return None
def findall(elem, path):
return list(iterfind(elem, path))
def findtext(elem, path, default = None):
el = find(elem, path)
if el is None:
return default
else:
return el.text